package com.common.mybatis;


import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.common.constant.CommonConstant;
import com.common.util.MyDateUtil;
import com.common.vo.LoginUser;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 *  <p> MyBatisPlus自定义字段自动填充处理类 - 实体类中使用 @TableField注解 </p>
 *
 * @description: 注意前端传值时要为null
 */

@Component //这个是springboot中使用的，SSM中在XML中配置好了
public class MyMetaObjectHandler implements MetaObjectHandler {

    private static final String CREATE_DATE = "createDate";
    private static final String UPDATE_DATE = "updateDate";
    private static final String CREATE_ID = "createId";
    private static final String UPDATE_ID = "updateId";


    /**
     * 创建时间
     */
    @Override
    public void insertFill(MetaObject metaObject) {
    	Object createDate = getFieldValByName(CREATE_DATE, metaObject);
    	Object updateDate = getFieldValByName(UPDATE_DATE, metaObject);
        if(createDate == null || createDate.equals(StrUtil.EMPTY)) {
            setFieldValByName(CREATE_DATE, MyDateUtil.getStrCurrentDate(MyDateUtil.sfh_ymdhms), metaObject);
        }
        if(updateDate == null || updateDate.equals(StrUtil.EMPTY)) {
        	setFieldValByName(UPDATE_DATE, MyDateUtil.getStrCurrentDate(MyDateUtil.sfh_ymdhms), metaObject);
        }
        Object createId = getFieldValByName(CREATE_ID, metaObject);
        Object updateId = getFieldValByName(UPDATE_ID, metaObject);
        if(createId == null || createId.equals(StrUtil.EMPTY)) {
            LoginUser loginUser = getLoginUser();
            setFieldValByName(CREATE_ID, loginUser.getId(), metaObject);
        }
        if(updateId == null || updateId.equals(StrUtil.EMPTY)) {
            LoginUser loginUser = getLoginUser();
            setFieldValByName(UPDATE_ID, loginUser.getId(), metaObject);
        }
    }

    /**
     * 更新时间
     */
    @Override
    public void updateFill(MetaObject metaObject) {
    	Object updateDate = getFieldValByName(UPDATE_DATE, metaObject);
        if(updateDate == null || updateDate.equals(StrUtil.EMPTY)) {
            setFieldValByName(UPDATE_DATE, MyDateUtil.getStrCurrentDate(MyDateUtil.sfh_ymdhms), metaObject);
        }
        Object updateId = getFieldValByName(UPDATE_ID, metaObject);
        if(updateId == null || updateId.equals(StrUtil.EMPTY)) {
            LoginUser loginUser = getLoginUser();
            setFieldValByName(UPDATE_ID, loginUser.getId(), metaObject);
        }
    }

    /**
     * 获取当前登录用户信息
     */
    public LoginUser getLoginUser(){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        //HttpServletResponse response = servletRequestAttributes.getResponse();
        LoginUser obj = (LoginUser)request.getSession().getAttribute(CommonConstant.LOGIN_USER);
        if(ObjectUtil.isNull(obj)){
            obj = new LoginUser();
            obj.setId(0);
            obj.setName(StrUtil.EMPTY);
        }
        return obj;
    }


    

}
